package com.leen.user.service.impl;

import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultVO;
import com.leen.user.dto.YxUserBillDTO;
import com.leen.user.entity.YxUser;
import com.leen.user.entity.YxUserBill;
import com.leen.user.repository.YxUserBillRepository;
import com.leen.user.service.YxUserBillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class YxUserBillServiceImpl extends CrudServiceImpl<YxUserBill,Long> implements YxUserBillService {


    private YxUserBillRepository yxUserBillRepository;

    @Autowired
    public void setYxUserBillRepository(YxUserBillRepository yxUserBillRepository) {
        this.yxUserBillRepository = yxUserBillRepository;
        super.setCrudRepository(yxUserBillRepository);
    }

    @Override
    public ResponseVO search(YxUserBillDTO yxUserBillDTO) {
        //分页查询
        Pageable pageable= PageRequest.of(yxUserBillDTO.getPageNumber()-1,yxUserBillDTO.getPageSize());
        //构建查询对象
        Specification<YxUserBill>specification=new Specification<YxUserBill>() {
            @Override
            public Predicate toPredicate(Root<YxUserBill> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list=new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(yxUserBillDTO.getCategory())){
                    list.add(criteriaBuilder.like(root.get("category"),"%"+yxUserBillDTO.getCategory()+"%"));
                }
                if (StringUtils.isNotBlank(yxUserBillDTO.getType())){
                    list.add(criteriaBuilder.like(root.get("type"),"%"+yxUserBillDTO.getType()+"%"));
                }
                if (yxUserBillDTO.getPm()!=null){
                    list.add(criteriaBuilder.equal(root.get("pm"),yxUserBillDTO.getPm()));
                }
                if (yxUserBillDTO.getBeginTime()!=null&&yxUserBillDTO.getEndTime()!=null){
                    list.add(criteriaBuilder.between(root.get("createTime"),yxUserBillDTO.getBeginTime(),yxUserBillDTO.getEndTime()));
                }

                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<YxUserBill> page = this.yxUserBillRepository.findAll(specification, pageable);
        return new ResponseVO(page.getTotalElements(),page.getContent());
    }
}
